﻿using System;
using System.Collections.Generic;
using UwaySoft.EsheetServer.BizLogic;
using UwaySoft.EsheetServer.DataLayer;
using UwaySoft.EsheetServer.Help.Template;
using UwaySoft.EsheetServer.Help.WorkDAL;
using UwaySoft.EsheetServer.Help.Response;

namespace UwaySoft.EsheetServer.Help
{
    public class WorkHepler
    {
        private readonly IWorkDAL _NewSheetDAL;
        private readonly IWorkDAL _FollowUpOperDAL;
        private readonly IWorkDAL _RevisitNoticeDAL;
        private readonly IWorkDAL _CheckHangDAL;
        private readonly IWorkDAL _handleDAL;
        /// <summary>
        /// Sql帮助
        /// </summary>
        private ISqlHelper SqlHelper
        {
            get { return Application.SqlHelper; }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public WorkHepler()
        {
            _NewSheetDAL = new NewSheetDAL();
            _FollowUpOperDAL = new FollowUpOperDAL();
            _RevisitNoticeDAL = new RevisitNoticeDAL();
            _CheckHangDAL = new CheckHangDAL();
            _handleDAL = new HandleDAL();
        }
        /// <summary>
        /// 工单记录入库
        /// </summary>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public IResponse NewSheet(string xmlString)
        {
            try
            {
                return _NewSheetDAL.DoWork(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("将工单保存到数据库失败\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false,"-1","-1", exp.Message);
            }            
        }

        /// <summary>
        /// 撤单/附加意见/催单
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        public IResponse FollowUpOper(string xmlString)
        {
            try
            {
                return _FollowUpOperDAL.DoWork(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("将撤单/附加意见/催单保存到数据库失败\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false, "-1", "-1", exp.Message);
            }  
        }

        /// <summary>
        /// 处理意见
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        public IResponse HandleInfo(string xmlString)
        {
            try
            {
                return _handleDAL.DoWork(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("将撤单/附加意见/催单保存到数据库失败\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false, "-1", "-1", exp.Message);
            }
        }

        /// <summary>
        /// 回访确认
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        public IResponse RevisitNotice(string xmlString)
        {
            try
            {
                return _RevisitNoticeDAL.DoWork(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("将回访确认到数据库失败\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false, "-1", "-1", exp.Message);
            }  
        }

        /// <summary>
        /// 审批挂起申请信息
        /// </summary>
        /// <param name="xmlstring"></param>
        /// <returns></returns>
        public IResponse CheckHang(string xmlString)
        {
            try
            {
                return _CheckHangDAL.DoWork(xmlString);
            }
            catch (Exception exp)
            {
                Log.Debug(string.Format("将审批挂起申请信息保存到数据库失败\r\n{0}", xmlString));
                Log.Error(exp);
                return new NewSheetResponse(false, "-1", "-1", exp.Message);
            }  
        }
        /// <summary>
        /// 获取最新工单列表
        /// </summary>
        /// <param name="topN">记录数</param>
        /// <returns></returns>
        public List<NewSheetEntity> GetLastWorkOrder(int topN)
        {
            var tSql = string.Format(
                @"SELECT * FROM MOD_WORKORDERDEMO T WHERE ROWNUM < {0} ORDER BY T.UPDATETIME DESC", topN);
            var dataSet = SqlHelper.ExecuteDateSet(tSql);
            var list = new List<NewSheetEntity>();

            if(dataSet.Tables.Count >0)
            {
                var table = dataSet.Tables[0];
                foreach (System.Data.DataRow row in table.Rows)
                {
                    list.Add(new NewSheetEntity
                                 {
                                     AcceptMgr = row["AcceptMgr"].ToString(),
                                     AcceptNo = row["AcceptNo"].ToString(),
                                     AcceptPhone = row["AcceptPhone"].ToString(),
                                     Address = row["Address"].ToString(),
                                     ContactId1 = row["ContactId1"].ToString(),
                                     ContactId2 = row["ContactId2"].ToString(),
                                     ContactName = row["ContactName"].ToString(),
                                     Email = row["Email"].ToString(),
                                     FaultHappenDate = row["FaultHappenDate"].ToString(),
                                     Fax = row["Fax"].ToString(),
                                     LastDate = row["LastDate"].ToString(),
                                     MainCallNo = row["MainCallNo"].ToString(),
                                     SerialNo = row["SerialNo"].ToString(),
                                     ServiceClassId = row["ServiceClassId"].ToString(),
                                     ServiceContent = row["ServiceContent"].ToString(),
                                     State = row["State"].ToString(),
                                     UserGrade = row["UserGrade"].ToString(),
                                     UserName = row["UserName"].ToString()
                                 });
                }
            }

            return list;
        }
    }
}
